package cn.edu.csust.tysp.backend.common;


import cn.edu.csust.tysp.backend.exception.ErrorCode;

/**
 * @Auther: snail
 * @Date: 2021/1/22 00:30
 * @Description: 基础响应对象（响应数据）
 */
public class ApiResult<T> extends ApiResponse{

    private static final String CODE = "0";

    private static final String MSG = "ok";

    private T data;

    public ApiResult(){
        super(Boolean.TRUE,CODE,MSG);
    }

    public ApiResult(T data){
        super(Boolean.TRUE,CODE,MSG);
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> ApiResult<T> ok(T data) {
        ApiResult result = new ApiResult();
        result.setCode(CODE);
        result.setMsg(MSG);
        result.setSuccess(Boolean.TRUE);
        result.setData(data);
        return result;
    }
    public static ApiResult error(ErrorCode errorCode){
        ApiResult result = new ApiResult();
        result.setCode(errorCode.getErrorCode());
        result.setMsg(errorCode.getErrorMsg());
        result.setSuccess(Boolean.FALSE);
        return result;
    }
}
